<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Home\Controller;
use Think\Controller;

class ScimgController extends Controller {

	public function index(){

		$this->display();
	}

	public function index1(){

		$this->display();
	}

	public function scimg1(){
		$name 		= $this->expnull(I('post.name_c'));

		$strl = strlen($name);
		if($strl == 6){
			$h = 210;
		}else if($strl == 6){
			$h = 190;
		}else{
			$h = 170;
		}
		$str_array = array(
			array('str' => $name, 		'coord' => array($h,705)),
		);

		$open_img 			= "./assets/img/yaoqing.jpg";	//原图
		$font_url 			= "./assets/img/fangsong.ttf";	//字体
		$font_size 			= 32;							//字号
		$font_color 		= "#fdad2f";					//字体颜色
		$arraylength		= count($str_array)-1;			//数组长度
		$imgname  			= time();

		if(!empty($name)){
			foreach($str_array as $key => $v){
				$imgurl = $this->save_img($v['str'], $v['coord'], $open_img, $font_url, $font_size, $font_color, $key, $arraylength, $imgname);
			}
			echo json_encode(array('code'=>0, 'data'=>$imgurl));
		}else{
			echo json_encode(array('code'=>1, 'data'=>null));
		}
	}

	public function scimg(){
		$name 		= $this->expnull(I('post.name_c'));
		$producer 	= $this->expnull(I('post.producer_c'));
		$tel 		= $this->expnull(I('post.tel_c'));
		$mobile 	= $this->expnull(I('post.mobile_c'));
		$email 		= $this->expnull(I('post.email_c'));
		$month 		= $this->expnull(I('post.month_c'));
		$day 		= $this->expnull(I('post.day_c'));

		$str_array = array(
			array('str' => $name, 		'coord' => array(270,435)),
			array('str' => $producer, 	'coord' => array(430,4800)),
			array('str' => $tel, 		'coord' => array(758,4750)),
			array('str' => $mobile, 	'coord' => array(758,4850)),
			array('str' => $email, 		'coord' => array(1208,4845)),
			array('str' => $month, 		'coord' => array(1324,4752)),
			array('str' => $day, 		'coord' => array(1392,4752))
		);

		$open_img 			= "./assets/img/daiyan.jpg";	//原图
		$font_url 			= "./assets/img/fangsong.ttf";	//字体
		$font_size 			= 26;							//字号
		$font_color 		= "#000000";					//字体颜色
		$arraylength		= count($str_array)-1;			//数组长度
		$imgname  			= time();

		if(!empty($name)){
			foreach($str_array as $key => $v){
				$imgurl = $this->save_img($v['str'], $v['coord'], $open_img, $font_url, $font_size, $font_color, $key, $arraylength, $imgname);
			}
			echo json_encode(array('code'=>0, 'data'=>$imgurl));
		}else{
			echo json_encode(array('code'=>1, 'data'=>null));
		}
	}

	/**
	 * @param $str			水印字符
	 * @param $coord		坐标
	 * @param $open_img		原图
	 * @param $font_url		字体路径
	 * @param $font_size	字体大小
	 * @param $font_color	字体颜色
	 */
	protected function save_img($str, $coord, $open_img, $font_url, $font_size, $font_color, $key, $arraylength, $imgname){
		$save_img  = "./assets/yqhan/".$imgname.".jpg";
		$image = new \Think\Image();
		if($key == 0){
			$img = $open_img;
		}else{
			$img = $save_img;
		}
		$image->open($img)->text($str, $font_url, $font_size, $font_color, $coord)->save($save_img);
		if($key == $arraylength){
			return "<img src='http://h5.hrtv.cn/assets/yqhan/".$imgname.".jpg'>";
		}
	}

	protected function expnull($str = null){
		$str = trim($str);
		return !empty($str) ? $str : '';
	}


}